package com.ourteam.product.model.dao.impl;

import com.ourteam.product.model.dao.IProductModelCodeTemplateDAO;
import com.ourteam.product.model.dao.ProductModelCodeTemplate;
import com.ourteam.product.model.dao.ProductModelCodeTemplateDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 IRKT 2017
 * Auto Generate Date : 2017-11-17 11:01:20
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductModelCodeTemplateDAOJdbcImpl
    extends AbstractSpringDAO implements IProductModelCodeTemplateDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, GenerateCodeType, TargetType, TemplateName, FileNameTemplate,
            FileType, FileContentTemplate, CanOverWrite, Status, Remarks,
            InputObjectType, FileOutputPath, TemplateVersion, LastModifyTime,
            LastMofiyUser, TemplateCode
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.GenerateCodeType,
            _DataObjectProperty.TargetType, _DataObjectProperty.TemplateName,
            _DataObjectProperty.FileNameTemplate, _DataObjectProperty.FileType,
            _DataObjectProperty.FileContentTemplate,
            _DataObjectProperty.CanOverWrite, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks, _DataObjectProperty.InputObjectType,
            _DataObjectProperty.FileOutputPath,
            _DataObjectProperty.TemplateVersion,
            _DataObjectProperty.LastModifyTime,
            _DataObjectProperty.LastMofiyUser, _DataObjectProperty.TemplateCode
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductModelCodeTemplate, ProductModelCodeTemplateDAOQueryBean> daoHelper;

    public AbstractProductModelCodeTemplateDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductModelCodeTemplate, ProductModelCodeTemplateDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductModelCodeTemplate
         * @throws Exception
         */
    public boolean checkIsUnique(
        ProductModelCodeTemplate aProductModelCodeTemplate)
        throws Exception {
        if (aProductModelCodeTemplate == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductModelCodeTemplate.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductModelCodeTemplate
     * @throws Exception
     */
    public void insert(ProductModelCodeTemplate aProductModelCodeTemplate)
        throws Exception {
        PropertyUtils.setProperty(aProductModelCodeTemplate,
            _DataObjectProperty.LastModifyTime,
            new java.sql.Timestamp(System.currentTimeMillis()));

        super.doInsert(aProductModelCodeTemplate);
    }

    /**
         * Batch Insert Datas
         * @param aProductModelCodeTemplate
         * @throws Exception
         */
    public void batchInsert(
        ProductModelCodeTemplate[] aProductModelCodeTemplates)
        throws Exception {
        super.doBatchInsert(aProductModelCodeTemplates);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductModelCodeTemplateDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductModelCodeTemplate aProductModelCodeTemplate)
        throws Exception {
        delete(aProductModelCodeTemplate.getId());
    }

    /**
     * Update Data Selective
     * @param aProductModelCodeTemplate
     * @throws Exception
     */
    public void update(ProductModelCodeTemplate aProductModelCodeTemplate)
        throws Exception {
        PropertyUtils.setProperty(aProductModelCodeTemplate,
            _DataObjectProperty.LastModifyTime,
            new java.sql.Timestamp(System.currentTimeMillis()));

        if (aProductModelCodeTemplate.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductModelCodeTemplate);

        ProductModelCodeTemplate tempObj = findById(aProductModelCodeTemplate.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductModelCodeTemplate, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductModelCodeTemplates[]
     * @throws Exception
     */
    public void batchUpdate(
        ProductModelCodeTemplate[] aProductModelCodeTemplates)
        throws Exception {
        super.doBatchUpdate(aProductModelCodeTemplates);
    }

    /**
         * Update Data By Condictions
         * @param aProductModelCodeTemplate
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductModelCodeTemplate aProductModelCodeTemplate,
        ProductModelCodeTemplateDAOQueryBean aQueryBean)
        throws Exception {
        PropertyUtils.setProperty(aProductModelCodeTemplate,
            _DataObjectProperty.LastModifyTime,
            new java.sql.Timestamp(System.currentTimeMillis()));

        super.doUpdateByQuery(aProductModelCodeTemplate, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductModelCodeTemplateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(
        ProductModelCodeTemplate aProductModelCodeTemplate)
        throws Exception {
        if (aProductModelCodeTemplate.getGenerateCodeType() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.generateCodeType value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getTargetType() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.targetType value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getTemplateName() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.templateName value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getFileNameTemplate() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.fileNameTemplate value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getFileType() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.fileType value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getFileContentTemplate() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.fileContentTemplate value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getCanOverWrite() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.canOverWrite value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getStatus() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.status value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getRemarks() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.remarks value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getInputObjectType() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.inputObjectType value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getFileOutputPath() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.fileOutputPath value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getLastModifyTime() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.lastModifyTime value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getLastMofiyUser() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.lastMofiyUser value is not allow NULL");
        }

        if (aProductModelCodeTemplate.getTemplateCode() == null) {
            throw new Exception(
                "The ProductModelCodeTemplate.templateCode value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductModelCodeTemplate findById(final long id)
        throws Exception {
        ProductModelCodeTemplateDAOQueryBean queryBean = new ProductModelCodeTemplateDAOQueryBean();

        ProductModelCodeTemplateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductModelCodeTemplate[] ProductModelCodeTemplates = query(queryBean);

        if (ProductModelCodeTemplates.length == 0) {
            return null;
        } else {
            return ProductModelCodeTemplates[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductModelCodeTemplate[]
         * @throws Exception
         */
    public ProductModelCodeTemplate[] query(
        ProductModelCodeTemplateDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductModelCodeTemplateRowMapper());

        return (ProductModelCodeTemplate[]) result.toArray(new ProductModelCodeTemplate[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductModelCodeTemplateDAOQueryBean queryBean)
        throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductModelCodeTemplateDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(
        ProductModelCodeTemplate[] aProductModelCodeTemplates,
        ProductModelCodeTemplateDAOQueryBean queryBean)
        throws Exception {
        daoHelper.synchronize(aProductModelCodeTemplates, queryBean);
    }

    public void synchronize(
        ProductModelCodeTemplate[] aProductModelCodeTemplates,
        ProductModelCodeTemplateDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductModelCodeTemplate> callBack)
        throws Exception {
        daoHelper.synchronize(aProductModelCodeTemplates, queryBean, callBack);
    }

    public static class JdbcProductModelCodeTemplateRowMapper
        implements RowMapper<ProductModelCodeTemplate> {
        public ProductModelCodeTemplate mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductModelCodeTemplate object = new ProductModelCodeTemplate();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object generateCodeType = null;

                generateCodeType = resultSet.getObject(columnIndex++);

                object.setGenerateCodeTypeNull(resultSet.getBoolean(
                        columnIndex++));

                if (generateCodeType != null) {
                    setBeanValue(object, _DataObjectProperty.GenerateCodeType,
                        generateCodeType);
                }

                Object targetType = null;

                targetType = resultSet.getObject(columnIndex++);

                object.setTargetTypeNull(resultSet.getBoolean(columnIndex++));

                if (targetType != null) {
                    setBeanValue(object, _DataObjectProperty.TargetType,
                        targetType);
                }

                Object templateName = null;

                templateName = resultSet.getObject(columnIndex++);

                object.setTemplateNameNull(resultSet.getBoolean(columnIndex++));

                if (templateName != null) {
                    setBeanValue(object, _DataObjectProperty.TemplateName,
                        templateName);
                }

                Object fileNameTemplate = null;

                fileNameTemplate = resultSet.getObject(columnIndex++);

                object.setFileNameTemplateNull(resultSet.getBoolean(
                        columnIndex++));

                if (fileNameTemplate != null) {
                    setBeanValue(object, _DataObjectProperty.FileNameTemplate,
                        fileNameTemplate);
                }

                Object fileType = null;

                fileType = resultSet.getObject(columnIndex++);

                object.setFileTypeNull(resultSet.getBoolean(columnIndex++));

                if (fileType != null) {
                    setBeanValue(object, _DataObjectProperty.FileType, fileType);
                }

                Object fileContentTemplate = null;

                fileContentTemplate = resultSet.getObject(columnIndex++);

                object.setFileContentTemplateNull(resultSet.getBoolean(
                        columnIndex++));

                if (fileContentTemplate != null) {
                    setBeanValue(object,
                        _DataObjectProperty.FileContentTemplate,
                        fileContentTemplate);
                }

                Object canOverWrite = null;

                canOverWrite = resultSet.getObject(columnIndex++);

                object.setCanOverWriteNull(resultSet.getBoolean(columnIndex++));

                if (canOverWrite != null) {
                    setBeanValue(object, _DataObjectProperty.CanOverWrite,
                        canOverWrite);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object inputObjectType = null;

                inputObjectType = resultSet.getObject(columnIndex++);

                object.setInputObjectTypeNull(resultSet.getBoolean(
                        columnIndex++));

                if (inputObjectType != null) {
                    setBeanValue(object, _DataObjectProperty.InputObjectType,
                        inputObjectType);
                }

                Object fileOutputPath = null;

                fileOutputPath = resultSet.getObject(columnIndex++);

                object.setFileOutputPathNull(resultSet.getBoolean(columnIndex++));

                if (fileOutputPath != null) {
                    setBeanValue(object, _DataObjectProperty.FileOutputPath,
                        fileOutputPath);
                }

                Object templateVersion = null;

                templateVersion = resultSet.getObject(columnIndex++);

                object.setTemplateVersionNull(resultSet.getBoolean(
                        columnIndex++));

                if (templateVersion != null) {
                    setBeanValue(object, _DataObjectProperty.TemplateVersion,
                        templateVersion);
                }

                Object lastModifyTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    lastModifyTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    lastModifyTime = resultSet.getObject(columnIndex++);
                }

                object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

                if (lastModifyTime != null) {
                    setBeanValue(object, _DataObjectProperty.LastModifyTime,
                        lastModifyTime);
                }

                Object lastMofiyUser = null;

                lastMofiyUser = resultSet.getObject(columnIndex++);

                object.setLastMofiyUserNull(resultSet.getBoolean(columnIndex++));

                if (lastMofiyUser != null) {
                    setBeanValue(object, _DataObjectProperty.LastMofiyUser,
                        lastMofiyUser);
                }

                Object templateCode = null;

                templateCode = resultSet.getObject(columnIndex++);

                object.setTemplateCodeNull(resultSet.getBoolean(columnIndex++));

                if (templateCode != null) {
                    setBeanValue(object, _DataObjectProperty.TemplateCode,
                        templateCode);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
